﻿using System;
using System.Collections.Generic;
using System.IO;

namespace nahida_desktop.Util
{
    public class PropertiesParser
    {
        private string fileUri;
        private Dictionary<string, object> propertyMap = new Dictionary<string, object>();

        public PropertiesParser(string fileUri) 
        { 
            if(fileUri == null)
            {
                throw new ArgumentNullException("fileUri is null");
            }
            this.fileUri = fileUri;
            parser();
        }

        public void parser()
        {
            StreamReader streamReader = null;
            try
            {
                streamReader = new StreamReader(fileUri);
                if(streamReader == null)
                {
                    throw new Exception("未找到该文件" + fileUri);
                }
                string line = null;
                while ((line = streamReader.ReadLine()) != null)
                {
                    string[] parts = line.Split(':');
                    if (parts.Length != 2)
                    {
                        throw new Exception("配置文件格式错误");
                    }
                    propertyMap.Add(parts[0], parts[1]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught: {0}", e);
            }
            finally
            {
                if(streamReader != null)
                {
                    streamReader.Close();
                }
            }
        }

        public object getValue(string key)
        {
            object value = propertyMap[key];
            return value;
        }

        public Dictionary<string, object> getPropertyMap() 
        {
            return propertyMap;
        }

    }
}

